<template>
	<div :class="curClassName" :style="curStyle">
		<a-input
			size="large"
			:class="editorClassName"
			:value="curValue"
			:type="defaultType"
			:disabled="disabled"
			:readonly="readonly"
			:placeholder="curCaption"
			@change="onChange"
			@blur="onBlur"
			@focus="onFocus"
		>
			<template #prefix v-if="defaultIcon == 'user-outlined'">
				<UserOutlined />
			</template>
			<template #prefix v-if="defaultIcon == 'key-outlined'">
				<KeyOutlined type="user" />
			</template>
		</a-input>
	</div>
</template>
<script setup lang="ts">
import { LoginInputProps } from './login-input';
import { UserOutlined, KeyOutlined } from '@ant-design/icons-vue';
import { IEvent } from '@/core';
import { Ref } from 'vue';
const props = defineProps(LoginInputProps);

interface LoginInputEmit {
	(name: 'component-action', value: IEvent<string>): void;
}
const emit = defineEmits<LoginInputEmit>();

/**
 * 项名称
 *
 * @memberof AppLoginInput
 */
const itemName = computed(() => {
	return props.index || props.index === 0
		? `${props.name}_${props.index}`
		: props.name;
});

/**
 * 标题
 *
 * @memberof AppLoginInput
 */
const curCaption = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.caption;
	}
});

/**
 * 编辑器类名
 *
 * @memberof AppLoginInput
 */
const editorClassName = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.getEditorDynaClass();
	}
});

/**
 * 当前容器样式
 *
 * @memberof AppLoginInput
 */
const curStyle = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.getElementStyle();
	}
});

/**
 * 类名
 *
 * @memberof AppLoginInput
 */
const curClassName = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	const classNames = {
		'app-login-input': true,
	};
	if (layoutModel) {
		Object.assign(classNames, layoutModel.getClassNames());
	}
	return classNames;
});

/**
 * 禁用
 *
 * @memberof AppLoginInput
 */
const disabled = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.disabled;
	}
});

/**
 * 只读
 *
 * @memberof AppLoginInput
 */
const readonly = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.readonly;
	}
});

/**
 * 当前值
 *
 * @type {any}
 * @memberof AppLoginInput
 */
let curValue: Ref<any> = computed(() => {
	return props.value;
});

/**
 * 值改变
 *
 * @memberof AppLoginInput
 */
const onChange = ($event: any) => {
	let value = $event.target.value;
	emit('component-action', {
		name: props.name,
		action: 'valueChange',
		data: value,
	});
};

/**
 * 聚焦
 * @param $event
 * @memberof AppLoginInput
 */
const onFocus = ($event: any) => {
	emit('component-action', {
		name: props.name,
		action: 'panelItemFocus',
		data: props.value,
	});
};

/**
 * 失焦
 * @param $event
 * @memberof AppLoginInput
 */
const onBlur = ($event: any) => {
	emit('component-action', {
		name: props.name,
		action: 'panelItemBlur',
		data: props.value,
	});
};
</script>
